<script setup lang="ts">
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import useUserStore  from '@/stores/user'
import { useRouter } from 'vue-router'


const router = useRouter()
const user = useUserStore()
const loginRef = ref<FormInstance>()
const loginForm = ref({
  username: '',
  password: '',
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度在6-16位之间', trigger: 'blur' },
  ],
}

// 登录提交功能
const onSubmit = () => {
  // 1.获取数据后再次校验   兜底校验
  loginRef.value!.validate(async (valid) => {
    if (!valid) return

    // 登录成功
    await user.login(loginForm.value)
    // 跳转到后台主页
    router.push('/')

    // // 测试登录业务逻辑
    // const loginUrl = 'http://localhost:3000/ftyyptapi/user/login'
    // const res = axios.post(loginUrl, loginForm.value)
    // console.log(res)
  })

  // 2.提交ajax请求
}
</script>

<template>
  <div class="login-container">
    <div class="loginBox">
      <div class="left">
        <img src="@/assets/images/loginBg.png" alt="" />
      </div>
      <div class="right">
        <div class="title">
          <img src="@/assets/images/logo.png" alt="" />
          锋团运营平台
        </div>

        <!-- 登录表单 
          element-plus 表单有三层的结构
          校验规则：
            el-form：model rules
            el-form-item: prop
            el-input:v-model 
        -->
        <el-form :model="loginForm" :rules="loginRules" ref="loginRef">
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  width: 100vw;
  height: 100vh;
  background-color: #eee;
  padding: 25px;

  .loginBox {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: #fff;
    padding: 0 100px;

    .left {
      flex: 1;
      img {
        width: 100%;
        object-fit: cover;
      }
    }

    .right {
      width: 500px;
      padding: 40px;
      border-radius: 10px;
      box-shadow: 0 2px 12px #0000001a;

      .title {
        font-size: 50px;
        font-weight: 700;
        margin-bottom: 30px;
        img {
          width: 50px;
        }
      }

      .el-input {
        height: 40px;
      }

      .el-button {
        width: 100%;
        height: 40px;
      }
    }
  }
}
</style>
